package com.bougaga.opensource.scheduler.web.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 健康检查和调试控制器
 * 
 * @author bougaga
 * @since 1.0.0
 */
@RestController
public class HealthController {
    
    private static final Logger logger = LoggerFactory.getLogger(HealthController.class);
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public Map<String, Object> health() {
        logger.debug("Health check requested");
        
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("timestamp", System.currentTimeMillis());
        health.put("service", "Bou Scheduler Web");
        
        return health;
    }
    
    /**
     * 调试信息
     */
    @GetMapping("/debug")
    public Map<String, Object> debug() {
        logger.debug("Debug info requested");
        
        Map<String, Object> debug = new HashMap<>();
        
        // 获取当前认证信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated()) {
            debug.put("authenticated", true);
            debug.put("username", authentication.getName());
            debug.put("authorities", authentication.getAuthorities());
            
            if (authentication.getPrincipal() instanceof UserDetails) {
                UserDetails userDetails = (UserDetails) authentication.getPrincipal();
                debug.put("userDetails", userDetails.getUsername());
                debug.put("enabled", userDetails.isEnabled());
                debug.put("accountNonExpired", userDetails.isAccountNonExpired());
                debug.put("credentialsNonExpired", userDetails.isCredentialsNonExpired());
                debug.put("accountNonLocked", userDetails.isAccountNonLocked());
            }
        } else {
            debug.put("authenticated", false);
        }
        
        // 系统信息
        debug.put("javaVersion", System.getProperty("java.version"));
        debug.put("osName", System.getProperty("os.name"));
        debug.put("userDir", System.getProperty("user.dir"));
        
        return debug;
    }
    
    /**
     * 会话信息
     */
    @GetMapping("/session")
    public Map<String, Object> session() {
        logger.debug("Session info requested");
        
        Map<String, Object> session = new HashMap<>();
        
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            session.put("sessionId", authentication.hashCode());
            session.put("authenticated", authentication.isAuthenticated());
            session.put("principal", authentication.getPrincipal().toString());
        }
        
        return session;
    }
}
